<?php

namespace App\Http\Controllers\Open\V2;

use App\Http\Controllers\Controller;
use App\Services\ImageResizeService;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
class TestController extends Controller
{

    public function __construct()
    {


    }

//    public function index() {
//        $user = User::find(3);
//        $token = $user->createToken('Api Token')->plainTextToken;
//        dd($token);
//
//    }

    public function test() {
        $size =[
            [120,100],
            [800,600],
            [1200,800],
        ];
        //取随机数组
        $x = rand(0,2);
        $url ="https://picsum.photos/".$size[$x][0]."/".$size[$x][1]."?random=".rand(10,99);
        $content = file_get_contents($url);
        $uniqueFileName = tempnam(sys_get_temp_dir(), 'tmp');  //临时文件
        $imageService = new ImageResizeService();
        //修改文件后缀
        if ($uniqueFileName) {
            $jpgFileName = str_replace('.tmp','.jpg',$uniqueFileName);
            rename($uniqueFileName,$jpgFileName);
            file_put_contents($jpgFileName, $content);
//            dd($jpgFileName);
//            dd($jpgFileName);

            $imageService->resizeImage($jpgFileName);
        }else {
            throw new \Exception('临时文件创建失败');
        }
        return $uniqueFileName;
    }

    public function getUser(Request $request)
    {
        // 使用 dd() 来调试和显示用户信息
        dd($request->user());

        // 或者直接返回用户信息
        return $request->user();
    }

}
